package 字符串;

import java.util.Arrays;

/**
 * @author 帅小伙
 * @date 2021/12/23
 * @description  第一个只出现一次的字符串
 */
public class Demo07 {

    public int FirstNotRepeatingChar(String str) {
        int res = -1;

        char[] chars = str.toCharArray();
        int[] arr = new int[26*2];
        int[] isUsed = new int[26*2];
        Arrays.fill(arr,Integer.MAX_VALUE);
        for ( int i = 0; i < chars.length; i++) {
            char c = chars[i];
            int index = -1;
            if(c>='a' && c <='z'){
                index = c-'a';
            }else{
                index = c-'A'+26;
            }
           if(isUsed[index]==0){
               arr[index] = i;
               isUsed[index] ++;
           }else{
               arr[index] = Integer.MAX_VALUE;
           }
        }

        int min = Integer.MAX_VALUE;
        for (int i = 0; i < 26*2; i++) {
            if(arr[i] < min){
                min = arr[i];
            }
        }
        if(min == Integer.MAX_VALUE){
            return -1;
        }else{
            return min;
        }

    }
}
